/**
 * 忘记密码
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/5
 */
var self = pic = '';

var index = 1;

var files = [];

document.addEventListener('plusready' , ready ,  false);

function ready(){
	
	self = plus.webview.currentWebview(); 
	
	var memberInfo = plus.storage.getItem('memberInfo');
	
	memberInfo = $.parseJSON(memberInfo);
	
	$('.upload').on('click' , function(){
		
		plus.nativeUI.actionSheet( {cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]}, function(e){
			if(e.index==1){
				var cmr = plus.camera.getCamera();
				cmr.captureImage(function(path) {
					appendPic(path);
				}, function(err) {});
				
			}else if(e.index==2){
				plus.gallery.pick(function(path){
					appendPic(path);
			    });
			}
		});
	})
	
	//提交反馈意见
	$('.feedbacks').on('click' , function(){ 
		
		var phone = $('input[name=phone]').val() , content = $('.body').val();
		
		if(!p1.test(phone)){
			
			toast('手机号码格式有误'); return;
			
		}
		
		if(!content.trim()){
			
			toast('请输入内容');return;
			
		}
		
		
		
   		$('.pic img').each(function(index){
   			
   			pic += pic == 0 ? $(this).attr('data-src') + '|,|' : '|-|' + $(this).attr('data-src');
   			
   		})
   		
		plus.nativeUI.showWaiting('发布中...');
		
		$.ajax({
			
			type:"post",
			
			url:apiRoot +'/Home/Message/addMessage/',
			
			data : {
				
				user : memberInfo.nickname,
				
				title : memberInfo.nickname +'在' + new Date().toLocaleString() + '发了一份反馈意见',
				
				uid : memberInfo.aid,
				
				phone : phone,
				
				pic : pic,
				
				body : content ,
				
				tid : 11
				
			},
			success : function(data){
				
				plus.nativeUI.closeWaiting();
				
				if(data > 0){
					
					$('input[name=phone]').val('');
					
					$('.content').html('');
					
					toast('发布成功');
					 
					self.close();
				}
			},
			error : function(e){
				
				console.log(JSON.stringify(e));
				
				plus.nativeUI.closeWaiting();
				
				toast('连接超时,请检查网络');
				
			}
		});
	})
	
}

function appendPic(p) {  
	var newPath = p.replace(/\./g , new Date().getTime()+'.');
	plus.zip.compressImage(
		{
			src:p,
			dst:newPath,
			quality:20
		},
		function() {
			files.push({name:"uploadkey"+index,path:p});
			index++;
			upload();
		},function(error) {
			toast('压缩图片失败');
		}
	);
}

var pics = {};

function upload(){
	plus.nativeUI.showWaiting('图片处理中...');
	if(files.length <= 0){  
		toast("请上传头像！");
		return;
	}
	var server = apiRoot+'/Home/Member/uploadPic/'; 
	var task = plus.uploader.createUpload(server,
		{method:"POST"},
		function(t,status){ //上传完成
			plus.nativeUI.closeWaiting();
			if(status == 200){
				toast('处理完成');
				$('.pic').prepend('<div class="image-item"><div class="image-close">X</div><img src="'+getImgUrl(t.responseText)+'" data-src="'+t.responseText+'"></div>');
				$('.image-close').on('click' , function(){ 
					$(this).parent().remove(); 
				})
			}else{
				plus.nativeUI.alert("上传失败："+status);
				plus.nativeUI.closeWaiting();
			}
		} 
	);
	for(var i = 0;i < files.length ; i++){
		var f = files[i];
		task.addFile(f.path,{key:f.name});
	} 
	task.start();
}

//function upload(){
//	plus.nativeUI.showWaiting('图片处理中...');
//	if(files.length <= 0){  
//		toast("请上传头像！");
//		return;
//	}
//	var server = apiRoot+'/Home/Member/uploadPic/'; 
//	var task = plus.uploader.createUpload(server,
//		{method:"POST"},
//		function(t,status){ //上传完成
//			plus.nativeUI.closeWaiting();
//			if(status == 200){
//				
//				toast('处理完成');
//				
//				$('.pic').find('div:first-child').find('img').attr('src' , getImgUrl(t.responseText)).attr('data-src' , t.responseText);
//				
// 				$('.image-close').on('click' , function(){ $(this).parent().remove(); })
//				
//			}else{
//				
//				plus.nativeUI.alert("上传失败："+status);
//				
//			}
//		} 
//	);
//	for(var i = 0;i < files.length ; i++){
//		var f = files[i];
//		task.addFile(f.path,{key:f.name});
//	} 
//	task.start();
//}